package com.bat.conf.feign;

import cn.hutool.core.collection.CollUtil;
import com.bat.util.TraceUtil;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.slf4j.MDC;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/**
 * MDC.getCopyOfContextMap()的使用
 * @Description:
 * @Author: zpf
 * @Date: 2019/11/7 下午12:59
 */
@Configuration
@ConditionalOnClass(RequestInterceptor.class)
public class TraceInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
        String tracdeid = TraceUtil.traceIdTreadLocal.get();
        template.header(TraceUtil.traceid, tracdeid);
        if (CollUtil.isNotEmpty(MDC.getCopyOfContextMap())) {
            for (Map.Entry<String, String> entry : MDC.getCopyOfContextMap().entrySet()) {
                template.header(entry.getKey(), entry.getValue());
            }
        }
    }
}
